<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 3/17/14
 * Time: 11:49 AM
 */

class Login extends CI_Controller {
    function index() {
        $this->load->view("admin/login");
    }

    function verfiy() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $username = trim($this->input->post('username'));
        $password = trim($this->input->post('password'));
        if ($username != null && $username != "" && $password != null && $password != "") {
            $query = $this->doctrine->em->createQuery('SELECT u FROM User u WHERE u.username = :username and u.password = :password');
            $query->setParameter('username', $username);
            $query->setParameter('password', $password);
            $users = $query->getResult();
            if ($users != null) {
                $sessionData = array("id" => $users[0]->getId(), "type" => "ADMIN");
                $this->session->set_userdata('logged_in', $sessionData);
                if ($this->session->userdata('logged_in')) {
                    echo true;
                }
            } else {
                echo false;
            }
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        $this->session->unset_userdata('logged_in');
//      delete_cookie("ci_session");
        redirect(base_url(), 'refresh');
    }
}